package org.letva.toink.blog.wicket;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.letva.toink.blog.model.Comment;
import org.letva.toink.blog.model.Post;
import org.letva.toink.blog.model.User;

public class UserDetailsPanel extends Panel {
    private User user;
    public UserDetailsPanel(String id, User user, HomePage.ShowHideAuthorPanelLink link) {
        super(id);
        this.user = user;
        add(new Label("username", this.user.getUsername()));

        ListView<Post> postListView = new ListView<Post>("posts", user.getPosts()) {

            @Override
            protected void populateItem(ListItem<Post> postListItem) {
                Post post = postListItem.getModelObject();
                postListItem.add(new Label("title", post.getTitle()));

                Integer commentCount = post.getComments() == null ? 0 : post.getComments().size();
                postListItem.add(new Label("commentCount", commentCount.toString()));
            }
        };
        add(postListView);

        ListView<Comment> commentListView = new ListView<Comment>("comments", user.getComments()) {

            @Override
            protected void populateItem(ListItem<Comment> commentListItem) {
                Comment post = commentListItem.getModelObject();
                commentListItem.add(new Label("text", post.getText()));
            }
        };
        add(commentListView);

        add(new ShowHidePanel("closePopup", link));

    }

    class ShowHidePanel extends AjaxLink {
        private AjaxLink link;
        public ShowHidePanel(String id, HomePage.ShowHideAuthorPanelLink authorLink) {
            super(id);
            link = authorLink;
        }

        @Override
        public void onClick(AjaxRequestTarget target) {
            link.onClick(target);
        }
    }
}
